package com.jiepu.daily.corejava.advanceed.part6thread.tortoiseandrabbitrace2;

import org.jetbrains.annotations.NotNull;

/**
 * @Description:
 * @author: xinjie.txt
 * @date: 2020/7/30  10:11
 */
public class TorANDRabRace2_2 {

	static class RaceThread extends Thread {


		long time = 0L;
		int sum = 0;
		int meter = 0;
		long t1 = System.currentTimeMillis();

		public RaceThread(@NotNull String name) {
			super(name);
		}

		@Override
		public void run() {

			if ("Tortoise".equals(getName())) {

				while (sum < 100) {
					meter = (int) (Math.random() * 3 + 2);
					sum += meter;
					System.out.println(getName() + "This Turn:  " + meter + " Total Instance : " + sum);

				} // while


			} // Tortoise


			else if ("Rabbit".equals(getName())) {
				while (sum < 100) {
					int sleep = (int) (Math.random() * 100);
					if (sleep < 30) {
						System.out.println(getName() + " Sleepd !!!");
						try {
							Thread.sleep(300);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}

					} else {
						meter = (int) (Math.random() * 3 + 7);
						sum += meter;
						System.out.println(getName() + "This Turn:  " + meter + " Total Instance : " + sum);

					}
				}


			} // Rabbit


		} // run

	} // Race

	public static void main(String[] args) {
		new RaceThread("4561");
		RaceThread rabThread1 = new RaceThread("Rabbit");
		RaceThread torThread2 = new RaceThread("Tortoise");
		rabThread1.start();
		torThread2.start();
		try {
			torThread2.join();
			rabThread1.join();

		} catch (InterruptedException e) {
			e.printStackTrace();
		}


	}
}
